<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:include="include/admin/adminHeader::html('后台首页')">
</head>
<!--<script src="webjars/jquery/2.0.0/jquery.min.js"></script>-->
<!--<link rel="stylesheet" href="webjars/bootstrap/3.0.1/css/bootstrap.min.css">-->
<!--<script src="webjars/bootstrap/3.0.1/js/bootstrap.min.js"></script>-->


<body>
<div id="workingArea" class="container-fluid">
    <!--导航栏-->
    <div th:replace="include/admin/adminNavigator::html"></div>

    <!--左侧折叠菜单-->
    <div th:include="include/admin/adminLeftMenu::html"></div>


    <div class="col-md-10">
        <ol class="breadcrumb">
            <li class="active">教学分类</li>
            <!--<li><a href="#">Library</a></li>-->
            <!--<li class="active">Data</li>-->
        </ol>

        <table class="table table-bordered table-responsive ">
            <thead>
            <tr>
                <th>ID</th>
                <th>分类名称</th>
                <th>分类描述</th>
                <!--<th>宣传图片</th>-->
                <th>开设课程</th>
                <th>删除</th>
                <th>编辑</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="bean in beans">
                <td>{{bean.id}}</td>
                <td>{{bean.name}}</td>
                <td>
                    <p style="width: 100px;white-space: nowrap;text-overflow: ellipsis; overflow: hidden;"
                       :title="bean.description">{{bean.description}}</p>
                </td>
                <!--<td>-->

                    <!--<div v-if="bean.img!=null">-->
                        <!--<img height="20px" width="20px" :src="bean.img">-->
                    <!--</div>-->
                    <!--<div v-else> null</div>-->
                <!--</td>-->
                <td><a :href="'subClass?cid='+bean.id"><span class="glyphicon glyphicon-list"></span></a></td>
                <td><a href="#" @click="deleteBean(bean.id)"><span class="glyphicon glyphicon-trash"></span></a></td>
                <td><a href="#" @click="getBean(bean.id)" data-toggle="modal" data-target="#myModal"><span
                        class="glyphicon glyphicon-edit"></span></a></td>
            </tr>
            </tbody>
        </table>
        <div th:replace="include/admin/adminPage::html"></div>
    </div>
    <div th:replace="include/admin/adminFoot::html"></div>


    <!--模态框-->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">修改分类信息</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group">
                            <label for="inputName" class="col-sm-2 control-label">Name</label>
                            <div class="col-sm-10">
                                <input type="text" name="name" v-model="bean.name" class="form-control"
                                       id="inputName" placeholder="Name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputDescription" class="col-sm-2 control-label">Description</label>
                            <div class="col-sm-10">
                                <input type="text" name="description" v-model="bean.description" class="form-control"
                                       id="inputDescription" placeholder="Description">
                            </div>
                        </div>
                        <!--<div class="form-group">-->
                            <!--<label class="col-md-4 control-label">宣传图片：</label>-->
                            <!--<div class="col-md-6">-->
                                <!--<input type="file" @change="getFile($event)" >-->
                            <!--</div>-->
                        <!--</div>-->
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button id="upload" type="button" class="btn btn-primary" @click="update" >Save changes
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    $(function () {
        var data4Vue = {
            uri: "category",
            pagination: {},
            beans: [],
            bean: {id: 0, name: "", description: "",img:""},
            file:""
        };

        var vue = new Vue({
            el: "#workingArea",
            data: data4Vue,
            mounted: function () {
                this.list(0);
            },
            methods: {
                list: function (start) {
                    var url = this.uri + "?start=" + start;
                    axios.get(url).then(function (response) {
                        vue.beans = response.data.data.content;
                        console.log(vue.beans);
                        vue.pagination = response.data.data;
                    })
                },
                getBean: function (id) {
                    var url = this.uri + "/" + id;
                    axios.get(url).then(function (response) {
                        vue.bean = response.data.data;
                    })
                },

                deleteBean: function (id) {
                    /*级联删除的提示*/
                    var confirmDelete = confirm("此操作将同时删除该分类下的所有子分类");
                    if (confirmDelete) ;
                    else
                        return;
                    var url = this.uri + "/" + id;
                    axios.delete(url).then(function (response) {
                        if (response.data.code == 0) {
                            vue.list(0);
                        }
                        else
                            alert(response.data.message);
                    })
                },
                update: function () {
                    /*bootstrap提交验证*/
                    var validator= $('form').data('bootstrapValidator').validate();
                    if(!validator.isValid()){
                        //没有通过校验
                        return;
                    } else {
                        //通过校验，可进行提交等操作
                    }
                    var formData=new FormData();
                    formData.append("id",this.bean.id);
                    formData.append("name",this.bean.name);
                    formData.append("description",this.bean.description);
                    formData.append("img",this.bean.img);
                    formData.append("file",this.file);
                    axios.put(this.uri, formData).then(function (response) {
                        if (response.data.code == 0) {
                            alert("修改成功");
                            vue.list(0);
                        }
                        else
                            alert("修改失败")
                    })

                    $('#upload').attr("data-dismiss","modal");
                },
                getFile:function (event) {
                    this.file=event.target.files[0];
                    console.log(this.file);
                },
                jump: function (page) {
                    jump(page, vue); //定义在adminHeader.html 中
                },
                jumpByNumber: function (start) {
                    jumpByNumber(start, vue);
                }
            }
        })
    })
</script>

<!--表单校验-->
<script>
    $(function () {
        $('form').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
//                valid: 'glyphicon glyphicon-ok',
//                invalid: 'glyphicon glyphicon-remove',
//                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                /*教学分类*/
                name:{
                    validators:{
                        notEmpty:{
                            message:'教学性质名称不能为空'
                        }
                    }
                },

            }
        });
    });
</script>
</html>